﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_Fin_Fin002c : AdminBasePage
{
    decimal boxID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[VBoxReceiptsMetadata.ColumnNames.BoxID] != null)
        {
            boxID = decimal.Parse(Request.QueryString[VBoxReceiptsMetadata.ColumnNames.BoxID]);
        }

        if (!IsPostBack)
        {
            drpYear.Items.Add(new ListItem(DateTime.Now.AddYears(-1).Year.ToString(), DateTime.Now.AddYears(-1).Year.ToString()));
            drpYear.Items.Add(new ListItem(DateTime.Now.Year.ToString(), DateTime.Now.Year.ToString()));
            drpYear.SelectedValue = DateTime.Now.Year.ToString();

            drpYear.Enabled = false;
            FinBox box = new FinBox();
            box.LoadByPrimaryKey(boxID);
            drpYear.SelectedValue = box.BoxYear.ToString();
            drpDocType.SelectedValue = box.DocumentType;
            drpLocation.SelectedValue = box.BoxLocation.ToString();
            drpCompany.SelectedValue = box.CompanyCode;
        }
        
    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        FinReceiptQuery q1 = new FinReceiptQuery("q1");

        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        BFThirdUnitQuery q5 = new BFThirdUnitQuery("q5");

        q1.LeftJoin(q2).On(q1.FunctionDept == q2.Code);
        q1.LeftJoin(q3).On(q1.StorageStatus == q3.Code);
        q1.LeftJoin(q4).On(q1.DocumentsType == q4.Code);
        q1.LeftJoin(q5).On(q1.StorageLocation == q5.ThirdUnitID);

        q1.SelectAll().Select(q2.Value1.As("FunctionDeptStr"), q3.Value1.As("StorageStatusStr"), q4.Value1.As("DocumentsTypeStr"), q5.Name.As("StorageLocationStr"));
        q1.Where(q1.CompanyCode == drpCompany.SelectedValue && q1.StorageLocation == drpLocation.SelectedValue && q1.DocumentsType == drpDocType.SelectedValue &&q1.StorageStatus == ConstantValue.StorageStatusCode.InStock
            && (q1.BoxID<0 || q1.BoxID.IsNull()));

        DateTime boxDate = new DateTime(int.Parse(drpYear.SelectedValue), 1, 1);
        q1.Where( q1.IsConfirmed == (int)YesOrNo.Yes);

        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        try
        {
            lbMessage.Text = "";

            FinBox box = new FinBox();
            box.LoadByPrimaryKey(boxID);
            FinReceipt fr = new FinReceipt();
            FinBoxReceipts fbr = new FinBoxReceipts();

            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (RepeaterItem item in pageGrid.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("chk_sel");
                    if (chkBox.Checked)
                    {
                        fbr = new FinBoxReceipts();
                        Label lbReceipt = (Label)item.FindControl("lbReceiptID");
                        fbr.BoxID = boxID;
                        fbr.ReceiptID = decimal.Parse(lbReceipt.Text);
                        fbr.UpdatedBy = fbr.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                        fbr.UpdatedDttm = fbr.CreatedDttm = DateTime.Now;
                        fbr.Save();
                        fr.LoadByPrimaryKey(decimal.Parse(lbReceipt.Text));
                        fr.BoxID = boxID;
                        fr.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                        fr.FirstStorageDate = DateTime.Now;
                        fr.Save();
                    }
                }
                box.BoxNo = GetBoxNo(boxID);
                box.Save();
                tr.Complete();

            }
            WebHelper.CloseDialog(true);

        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
}